package pl.gettoknowme.mobile.android.model.dao;

import java.util.ArrayList;
import java.util.List;

import pl.gettoknowme.mobile.android.model.dbtypes.Integration;
import pl.gettoknowme.mobile.android.model.dbtypes.Integration.IntegrationType;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Obiekt DAO dla tabeli 'integration'
 * @author Michał Kołodziejski
 */
public class IntegrationDAO {

    private static final String TABLE_NAME = "integration";
    private static final String[] COLUMNS = {
        "id",
        "integration_type",
        "is_successful",
        "integration_timestamp"
    };
    
    /**
     * Uchwyt do bazy danych
     */
    private SQLiteDatabase db;

    /**
     * Tworzy obiekt DAO dla tabeli 'integration'
     * @param db uchwyt do bazy danych
     */
    public IntegrationDAO(SQLiteDatabase db) {
        this.db = db;
    }
    
    
    /**
     * Zwraca listę wszystkich integracji posotrowanych po czasie zebrania
     * @return lista wszystkich integracji
     */
    public List<Integration> getIntegrationList(){
        List<Integration> result = new ArrayList<Integration>();
        Cursor cursor = db.query(TABLE_NAME, COLUMNS, null, null, null, null, COLUMNS[3]);
        
        while(cursor.moveToNext()){
            result.add(readIntegrationFromCursor(cursor));
        }
        
        cursor.close();
        return result;
    }
    
    
    /**
     * Zwraca listę wszystkich integracji danego typu posotrowanych po czasie zebrania
     * @return lista wszystkich integracji
     */
    public List<Integration> getIntegrationList(IntegrationType integrationType){
        List<Integration> result = new ArrayList<Integration>();
        Cursor cursor = db.query(TABLE_NAME, 
                COLUMNS, 
                COLUMNS[1] + " = ?", 
                new String[] { integrationType.toString() }, 
                null, 
                null, 
                COLUMNS[3]);
        
        while(cursor.moveToNext()){
            result.add(readIntegrationFromCursor(cursor));
        }
        
        cursor.close();
        return result;
    }
    
    public Integration getLastSuccessfulIntegration(IntegrationType integrationType){
        Cursor cursor = db.query(TABLE_NAME, 
                COLUMNS, 
                COLUMNS[1] + " = ? AND " + COLUMNS[2] + " = 1", 
                new String[] { integrationType.toString() }, 
                null, 
                null, 
                COLUMNS[3] + " DESC LIMIT 1");
        
        if(cursor.moveToNext()){
            Integration integration = readIntegrationFromCursor(cursor);
            cursor.close();
            return integration;
        }
        
        cursor.close();
        return null;
    }
    
    /**
     * Wstawia dane nowej integracji
     * @param integration dane nowej integracji do dodania
     */
    public void insert(Integration integration){
        ContentValues contentValues = new ContentValues();
//        contentValues.put(COLUMNS[0], 1); // tu jest autoincrement
        contentValues.put(COLUMNS[1], integration.getIntegrationType().toString());
        contentValues.put(COLUMNS[2], integration.isSuccessful());
        contentValues.put(COLUMNS[3], integration.getIntegrationTime());
        db.insertOrThrow(TABLE_NAME, null, contentValues);
    }
    
    /**
     * Odczytuje dane pojedynczej integracji z bieżącej pozycji kursora
     * @param cursor kursor
     * @return dane pojedynczej integracji
     */
    private Integration readIntegrationFromCursor(Cursor cursor){
        Integration integration = new Integration();
        integration.setId(cursor.getInt(0));
        integration.setIntegrationType(IntegrationType.valueOf(cursor.getString(1)));
        integration.setSuccessful(cursor.getInt(2) == 1);
        integration.setIntegrationTime(cursor.getLong(3));
        return integration;
    }
}
